package com.zcf.shiro.web.controller;

import com.zcf.shiro.web.bean.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * @author zhaochaofeng
 * @date 2020/6/18 9:25
 */
public abstract class CommonController {

    /**
     * 获取登入用户
     * @return
     */
    public User getLoginUser() {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            Object object = subject.getPrincipal();
            if (object != null) {
                return (User) object;
            }
        }
        return null;
    }

    /**
     * 判断是否拥有某个权限
     * @param role
     * @return
     */
    public boolean hasRole(String role) {
        Subject subject = SecurityUtils.getSubject();
        if (subject != null) {
            return subject.hasRole(role);
        }else{
            return false;
        }
    }
}
